package cn.dgut.jy.soro.controller;

import cn.dgut.jy.soro.entity.User;
import cn.dgut.jy.soro.service.UserService;
import cn.dgut.jy.soro.service.MailService;
import cn.dgut.jy.soro.service.VerificationService;
import cn.dgut.jy.soro.util.SHA256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
public class UserController {
    private UserService userService;
    private MailService mailService;
    private VerificationService verificationService;

    @Autowired
    public UserController(UserService userService, MailService mailService,
                          VerificationService verificationService) {
        this.userService = userService;
        this.mailService = mailService;
        this.verificationService = verificationService;
    }

    //登录部分

    @RequestMapping(value = "/login")
    public String login(HttpSession session) {
        if(session.getAttribute("username") != null) {
            return "/index";
        }
        return "/login";
    }

    @RequestMapping(value = "/logging", method = RequestMethod.POST)
    public String logging(HttpServletResponse response, HttpServletRequest request, HttpSession httpSession, @RequestParam String mobile, @RequestParam String password, Model model) throws IOException {
        User user = userService.login(mobile, SHA256Util.getSHA256Str(password));
        if (user == null) {
            model.addAttribute("message", "账号或密码错误！");
            //response.sendRedirect(request.getContextPath() + "/login");
            return "/login";
        }
        //model.addAttribute("username", customer.getUsername());
        else {
            httpSession.setAttribute("username", user.getUsername());
            httpSession.setAttribute("userId", user.getId());
            //response.sendRedirect(request.getContextPath() + "/shop");
            return "/shop";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession httpSession) {
        httpSession.removeAttribute("username");
        httpSession.removeAttribute("userId");
        return "/login";
    }

    //注册部分
    @RequestMapping(value = "/joining", method = RequestMethod.POST)
    public String join(HttpSession httpSession,@RequestParam String username, @RequestParam String mobile,@RequestParam String email,@RequestParam String verification, @RequestParam String password, Model model) {
        Integer authority = 1;

        if(!verificationService.verifying(email, verification)) {
            model.addAttribute("message", "验证码错误！");
            return "/join";
        }

        if(!userService.join(username, mobile, email, password, authority)) {
            model.addAttribute("message", "请检查输入信息！");
            return "/join";
        }

        model.addAttribute("message", "注册成功");

        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/sendVerification", method = RequestMethod.POST)
    public void verifying(HttpSession session, @RequestParam String email) throws MessagingException, UnsupportedEncodingException {
        String verification = verificationService.createVerificationAndSave(email);
        mailService.sendMail(email, "账号验证", "您的验证码为： " + verification);
    }

    //以下是个人中心功能

    @RequestMapping("/personal")
    public String personal(Model model, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            return "/login";
        }
        User user = userService.getPersonalInfo(userId);
        model.addAttribute("username", (String) session.getAttribute("username"));
        model.addAttribute("email", user.getEmail());
        model.addAttribute("mobile", user.getMobile());
        if(user.getAuthority() == 2) {
            return "staffPersonal";
        }

        return "personal";
    }

    @RequestMapping("/changePassword")
    public String changePassword(HttpSession session, Model model) {
        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            model.addAttribute("mobile", "");
            model.addAttribute("email", "");
        } else {
            User user = userService.getPersonalInfo(userId);
            model.addAttribute("mobile", user.getMobile());
            model.addAttribute("email", user.getEmail());
        }

        return "/changePassword";
    }


    @RequestMapping(value = "/changing", method = RequestMethod.POST)
    public String changing(Model model, HttpSession session, @RequestParam String mobile,
                         @RequestParam String email,
                         @RequestParam String verification,
                         @RequestParam String newPassword) {
        if(!verificationService.verifying(email, verification)) {
            model.addAttribute("message", "验证码错误！");
            return "/changePassword";
        }

        if(!userService.changePassword(mobile, email, newPassword)) {
            model.addAttribute("message", "请检查输入信息！");
            return "/changePassword";
        }
        session.removeAttribute("username");
        session.removeAttribute("userId");
        return "/login";
    }
}
